Refactor TransactionManager key tracking to use SessionParseState and ScratchBufferAllocator#1638
Merged
Refactor TransactionManager key tracking to use SessionParseState and ScratchBufferAllocator#1638
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors transaction cluster-slot key tracking and WATCH key storage to avoid keeping raw pointers into the network receive buffer by copying key bytes into scratch buffers and reusing the SessionParseState key-processing path.
Changes:
- Replaced transaction cluster key tracking from
PinnedSpanByte[]+ pointer rebasing toSessionParseStatebacked by a dedicatedScratchBufferAllocator. - Refactored
WatchedKeysContainerto store watched key bytes viaScratchBufferAllocatorinstead of a manually-managedbyte[]buffer. - Added
SessionParseState.Capacityto support dynamic growth of the argument buffer.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| libs/server/Transaction/TxnWatchedKeysContainer.cs | Switch WATCH key byte storage to ScratchBufferAllocator and simplify reset/growth logic. |
| libs/server/Transaction/TxnRespCommands.cs | Update EXEC slot verification to use NetworkMultiKeySlotVerify(ref SessionParseState, ...) with ClusterSlotVerificationInput. |
| libs/server/Transaction/TxnClusterSlotCheck.cs | Replace raw key array tracking with SessionParseState + scratch-buffer-copied key slices. |
| libs/server/Transaction/TransactionManager.cs | Introduce txnScratchBuffer and clusterKeyParseState; update reset and slot verification input construction. |
| libs/server/Resp/Parser/SessionParseState.cs | Add Capacity accessor for argument buffer capacity checks. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
vazois
reviewed
Mar 19, 2026
vazois
approved these changes
Mar 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR replaces raw
PinnedSpanBytearrays and manual pointer management in transaction cluster slot verification and WATCH key storage withSessionParseStateandScratchBufferAllocator.Problem:
TransactionManagerstored keys for slot verification as rawPinnedSpanBytepointers into the network receive buffer. Because the receive buffer can be reallocated between MULTI and EXEC, a fragileUpdateRecvBufferPtrmethod was needed to walk all stored pointers and rebase them — prone to dangling pointer bugs.NetworkKeyArraySlotVerify(iteratingSpan<PinnedSpanByte>), while all other multi-key slot verification paths already use theSessionParseState-basedNetworkMultiKeySlotVerifyoverload.WatchedKeysContainermanaged its own rawbyte[]buffer with manual pointer arithmetic and pointer fixup on resize — duplicating logic thatScratchBufferAllocatoralready provides.watchContainer.Reset()in DISCARD and aborted EXEC paths left watched key slices pointing into invalidated scratch buffer memory (use-after-reset).Changes introduced:
PinnedSpanByte[] keys+keyCountwith aSessionParseState clusterKeyParseStateonTransactionManagerScratchBufferAllocator(txnScratchBuffer) lazily when a receive buffer reallocation is detectedUpdateRecvBufferPtr; replaced withCopyExistingKeysToScratchBuffertriggered bysaveKeyRecvBufferPtrchange detectionGetSlotVerificationInputbuildsClusterSlotVerificationInputdirectly; EXEC uses the standardNetworkMultiKeySlotVerify(ref SessionParseState, ...)pathCapacityproperty toSessionParseStateto support doubling growthclusterKeyParseState.Countis 0 (e.g., MULTI/EXEC with only non-key commands)byte[] buffer+ raw pointer arithmetic + pointer fixup on resize with a sharedScratchBufferAllocator(txnScratchBuffer)watchBuffer,watchBufferPtr,watchBufferHeadAddress,watchBufferSize,initialWatchBufferSizewatchContainer.Reset()to DISCARD and aborted EXEC paths to prevent use-after-reset of watched key slicesNetworkKeyArraySlotVerifyfromRespServerSession,IClusterSession, andClusterSession, along with itsSpan<PinnedSpanByte>MultiKeySlotVerifyoverload